package com.xiaochi.oss.adapter;

import com.xiaochi.oss.entity.FileInfo;
import com.xiaochi.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @author zqx
 * @since 2024/8/21
 */
public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;

    @Value("${minio.url}")
    private String url;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        if (objectName != null){
            minioUtil.uploadFile(multipartFile.getInputStream(),bucket,objectName+"/" + multipartFile.getOriginalFilename());
        }else {
            minioUtil.uploadFile(multipartFile.getInputStream(),bucket,multipartFile.getOriginalFilename());
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBuckets() {
        return minioUtil.getAllBuckets();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFiles(String bucket) {
        return minioUtil.getAllFiles(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream download(String bucket, String objectName) {
        return minioUtil.download(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucket, String objectName) {
        minioUtil.deleteFile(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }

    @Override
    public String getUrl(String bucket, String objectName) {
        return url + "/" + bucket + "/" + objectName;
    }
}
